package cn.btks.resultintegration.catalog.repository;

import cn.btks.resultintegration.catalog.pojo.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author lijun
 * @Date 2020/12/15 20:06
 */
@Repository
public class BaseCatalogRepository {

    @Autowired
    JdbcTemplate jdbcTemplate;

    /**
     * 批量插入目录信息信息
     * @param catalogList
     */
    public void insertCatalogs(List<Catalog> catalogList) {
        String sql = "insert into catalog(code,catalogName,level,upLevelId,catalogOrder) values (?,?,?,?,?)";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Catalog catalog = catalogList.get(i);
                preparedStatement.setString(1, catalog.getCode());
                preparedStatement.setString(2, catalog.getCatalogName());
                preparedStatement.setInt(3, catalog.getLevel());
                preparedStatement.setLong(4, catalog.getUpLevelId());
                preparedStatement.setLong(5, catalog.getCatalogOrder());
            }

            @Override
            public int getBatchSize() {
                return catalogList.size();
            }
        });
    }

    /**
     * 批量更新目录信息
     * @param catalogList
     */
    public void updateCatalogs(List<Catalog> catalogList) {
        String sql = "update catalog set code=?,catalogName=?,level=?,upLevelId=?,catalogOrder=? where id=?";
        jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement preparedStatement, int i) throws SQLException {
                Catalog catalog = catalogList.get(i);
                preparedStatement.setString(1, catalog.getCode());
                preparedStatement.setString(2, catalog.getCatalogName());
                preparedStatement.setInt(3, catalog.getLevel());
                preparedStatement.setLong(4, catalog.getUpLevelId());
                preparedStatement.setLong(5, catalog.getCatalogOrder());
                preparedStatement.setLong(6, catalog.getId());
            }

            @Override
            public int getBatchSize() {
                return catalogList.size();
            }
        });
    }
}
